
using System;

using PolePosition.v3.EntityLibrary;
namespace PolePosition.v3.EntityFactory
{
    /// <summary>
    /// 
    /// </summary>
	/// <remarks></remarks>
	public sealed partial class VendorInspVisitXmissionStructuresResultFactory 
	{		
		/// <summary>
		/// This static constructor is responsible for subscribing to all events in the class. 
		/// If Child Collections were created, then this constructor subscribes to events 
		/// that handle getting and saving child collections.
		/// </summary>
        /// <remarks><para>This constructor is present in VendorInspVisitXmissionStructuresResultFactory because VendorInspVisitXmissionStructuresResult Entities
        /// have Child Collection properties. Child Collection properties contain groups of additional Entities
        /// that in turn, contain data related to VendorInspVisitXmissionStructuresResult Entities. Whether Child Collections, this 
        /// constructor, and the event handlers it wires up are present was determined at the time this code was
        /// generated. See the documentation for the <see cref="Classes.Entities.VendorInspVisitXmissionStructuresResultEntity" /> for more.
        /// </para>
        /// <para>This constructor makes sure that all actions by the Factory that are relevant to Child Collections
        /// are responded to. This means subscribing to the events raised by any of the <c>Save()</c>, 
        /// <c>GetEntity()</c> and <c>GetCollection()</c> overloads (see the documentation for
        /// <see cref="Classes.Factories.VendorInspVisitXmissionStructuresResultFactory"/>). The event handlers assigned by this constructor
        /// ensure that Child Collections are properly retrieved, saved, or deleted (that is, preventing any concurrency
		/// violations) whenever the VendorInspVisitXmissionStructuresResultEntity they belong to is modified.</para>
        /// <para>This constructor assigns handlers to the following VendorInspVisitXmissionStructuresResultFactory
        /// events:</para>
        /// <list type="bullet">
            /// <item>"Get" events: <see cref="VendorInspVisitXmissionStructuresResultFactory.OnAfterGetEntity" /> and 
            /// <see cref="VendorInspVisitXmissionStructuresResultFactory.OnAfterGetEntityCollection" />. This ensures that when a 
            /// VendorInspVisitXmissionStructuresResultEntity is retrieved from the database, any Child Collections it needs are retrieved also.
            /// </item>
            /// 
            /// <item>"Save" events: <see cref="VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveEntityBegin" />, 
            /// <see cref="VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveCollectionBegin" />, and 
            /// <see cref="VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveEntityCommit" />. 
            /// Since transactions are used when saving Entities, subscription to the first two
            /// events ensures that only one transaction is started and that it is commited by the correct Factory 
            /// <c>Save()</c> method (see how the 
            /// <see cref="Classes.Factories.VendorInspVisitXmissionStructuresResultFactory.Save(Classes.Entities.VendorInspVisitXmissionStructuresResultEntity, bool)"/>
            /// method works).<para></para>
            /// Subscription to the third event ensures that when a VendorInspVisitXmissionStructuresResultEntity is saved, any Child Collections
            /// it contains are also saved and that they are saved in the correct order to avoid concurrency problems.
            /// </item>
            /// 
            /// <item>"Delete" events: <see cref="VendorInspVisitXmissionStructuresResultFactory.OnBeforeEntityDelete" />. This ensures that when
            /// a VendorInspVisitXmissionStructuresResultEntity is deleted, any Child Collections it contains are deleted first to avoid concurrency
			/// problems.
            /// </item>
        /// </list>
        /// <para>One event handler is assigned to each event for every Child Collection that needs to be present
        /// for a VendorInspVisitXmissionStructuresResultEntity. So, if the Entity has two Child Collections, two handlers are added to 
        /// each event; in that way, all collections will react when an event is raised. This constructor assigns
		/// handlers for:
		/// <list type="bullet">
        /// </list>
        /// </para>
		/// <para>Handler methods were generated for each event and for each collection type, and are also part
		/// of the VendorInspVisitXmissionStructuresResultFactory (they are all identified by a composed name that consists of: the
		/// event they respond to, the child collection they belong to, and the word "Handler" at the end.</para>
		/// <para>Note also that this constructor calls the 
		/// <see cref="Classes.Factories.VendorInspVisitXmissionStructuresResultFactory.CustomConstructor()"/> method at the end. Since
		/// code for that method is not generated by the code generation tool that generated this constructor, 
		/// calling that method allows for custom code to execute along with this constructor.</para>
        /// </remarks>
        static void ChildCollectionConstructor()
        {
			#region Wire Up Event Handling for VendorInspVisitXmissionStructuresImage Child Collection
			//Get Methods - wire up the get events so that when the parent class is 
			//aquired the children are also
            VendorInspVisitXmissionStructuresResultFactory.OnAfterGetEntity += VendorInspVisitXmissionStructuresResultFactory.OnAfterGetVendorInspVisitXmissionStructuresImageEntityHandler;
            VendorInspVisitXmissionStructuresResultFactory.OnAfterGetEntityCollection += VendorInspVisitXmissionStructuresResultFactory.OnAfterGetVendorInspVisitXmissionStructuresImageCollectionHandler;

            //Collection Save
            VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveCollectionBegin += VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveVendorInspVisitXmissionStructuresImageCollectionBeginHandler;

            //Entity Save
            VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveEntityBegin += VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveVendorInspVisitXmissionStructuresImageEntityBeginHandler;
            VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveEntityCommit += VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveVendorInspVisitXmissionStructuresImageEntityCommitHandler;

            //Entity Delete
            VendorInspVisitXmissionStructuresResultFactory.OnBeforeEntityDelete += VendorInspVisitXmissionStructuresResultFactory.OnBeforeVendorInspVisitXmissionStructuresImageEntityDeleteHandler;
			#endregion
		
		}
		
		#region Event Handlers: VendorInspVisitXmissionStructuresImage 
		
		/// <summary>
        /// Stores the number that indicates the order of the VendorInspVisitXmissionStructuresImage table in the
		/// DataSet returned by the Factory's <c>Get()</c> methods. 
        /// </summary>
		/// <remarks>This number is used by the <see cref="OnAfterGetVendorInspVisitXmissionStructuresImageEntityHandler" />
		/// and the <see cref="OnAfterGetVendorInspVisitXmissionStructuresImageCollectionHandler" /> methods to determine
		/// if the VendorInspVisitXmissionStructuresImage table exists in the DataSet. This number was
		/// determined at the time the code was generated.
		/// </remarks>
		private const int VendorInspVisitXmissionStructuresImageOrdinalPosition = 1;
		
		#region Get VendorInspVisitXmissionStructuresImage Event Handlers

		/// <summary>
        /// Handler for the <see cref="Classes.Factories.VendorInspVisitXmissionStructuresResultFactory.OnAfterGetEntity" /> event. Fills a collection 
		/// with <c>VendorInspVisitXmissionStructuresImage</c> Entities and adds it to the VendorInspVisitXmissionStructuresResultEntity passed in the event
		/// argument object.
		/// </summary>
		/// <exception cref="ProjectCommon2.Exceptions.MissingDataTableException">Thrown if the VendorInspVisitXmissionStructuresImage
		/// table from which the VendorInspVisitXmissionStructuresImage Collection will be filled does not exist in the DataSet 
		/// contained in the event argument. Thrown using 
		/// <see cref="ProjectCommon2.Helpers.ExceptionHelper.Throw(System.Exception)"/>, which controls
		/// exception handling activities.
		/// </exception>
        /// <param name="sender">The object that raised the event.</param>
        /// <param name="args">The object containing data about the event; must be of type 
		/// <see cref="Bases.AfterGetEntityEventArgs" />, as it contains the DataSet to fill the 
		/// collection with, and the Entity to which the collection will be added.</param>
		/// <remarks>
		/// <para>This method creates a new 
		/// <see cref="ProjectCommon2.Generics.EntityCollection&lt;EntityType&gt;" /> using data from the 
		/// DataSet passed in the event argument and then assigns it to the
		/// <see cref="Classes.Entities.VendorInspVisitXmissionStructuresResultEntity.VendorInspVisitXmissionStructuresImageCollection" /> property of the
		/// VendorInspVisitXmissionStructuresResultEntity being retrieved (also passed in the event argument).</para>
		/// </remarks>
        static void OnAfterGetVendorInspVisitXmissionStructuresImageEntityHandler(object sender, Classes.Bases.AfterGetEntityEventArgs args)
        {

            //ensure that the dataset contains the second datatable constaining
            //the VendorInspVisitXmissionStructuresImage data
            if (args.DataSetFromDataBase.Tables.Count < VendorInspVisitXmissionStructuresImageOrdinalPosition + 1)
            {
                ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataTableException("VendorInspVisitXmissionStructuresImage not available for addition to VendorInspVisitXmissionStructuresResult entity; The factory event handler found that there is no member ordinally placed in the dataset datatable collection."));
            }


            //redeclare and cast the argument result entity 
            VendorInspVisitXmissionStructuresResultEntity EntityToReturn;
            EntityToReturn = (VendorInspVisitXmissionStructuresResultEntity)args.EntityToReturn;

			//ensure the entity is created
			if (EntityToReturn == null)
				return;

            //create an object to generate the collection for addition
            //to the entity 
            ProjectCommon2.Generics.EntityCollection< VendorInspVisitXmissionStructuresImageEntity> NewCollection;

            //fill the collection with the datatable
            NewCollection = new ProjectCommon2.Generics.EntityCollection< VendorInspVisitXmissionStructuresImageEntity>(args.DataSetFromDataBase.Tables[VendorInspVisitXmissionStructuresImageOrdinalPosition],args.UseCheckSum);

            //add the collection to the entity
            EntityToReturn.VendorInspVisitXmissionStructuresImageCollection = NewCollection;

        }
		
		/// <summary>
        /// Handler for the <see cref="Classes.Factories.VendorInspVisitXmissionStructuresResultFactory.OnAfterGetEntityCollection" /> event. Adds a collection 
		/// of <c>VendorInspVisitXmissionStructuresImage</c> Entities to each VendorInspVisitXmissionStructuresResultEntity in the collection passed in the 
		/// event argument object.
		/// </summary>
		/// <exception cref="ProjectCommon2.Exceptions.MissingDataTableException">Thrown if the VendorInspVisitXmissionStructuresImage
		/// table from which the VendorInspVisitXmissionStructuresImage Collection will be filled does not exist in the DataSet 
		/// contained in the event argument. Thrown using 
		/// <see cref="ProjectCommon2.Helpers.ExceptionHelper.Throw(System.Exception)"/>, which controls
		/// exception handling activities.
		/// </exception>
        /// <param name="sender">The object that raised the event.</param>
        /// <param name="args">The object containing data about the event; must be of type 
		/// <see cref="Bases.AfterGetEntityEventArgs" />, as it contains the DataSet to fill the 
		/// collection with, and the Entities to which a VendorInspVisitXmissionStructuresImage collection will be added.</param>
		/// <remarks>
		/// <para>This method creates a new 
		/// <see cref="ProjectCommon2.Generics.EntityCollection&lt;EntityType&gt;" /> and fills it with VendorInspVisitXmissionStructuresImage
		/// Entities from the VendorInspVisitXmissionStructuresImage data contained in the DataSet passed in the event argument. It then filters
		/// this collection for each VendorInspVisitXmissionStructuresResultEntity (according to the Vendor_InspVisit_XmissionStructures_ResultId
		/// property), and assigns the result to the <see cref="Classes.Entities.VendorInspVisitXmissionStructuresResultEntity.VendorInspVisitXmissionStructuresImageCollection" />
		/// property of each VendorInspVisitXmissionStructuresResultEntity.</para>
		/// </remarks>
        static void OnAfterGetVendorInspVisitXmissionStructuresImageCollectionHandler(object sender, Classes.Bases.AfterGetEntityCollectionEventArgs args)
        {
            //ensure that the dataset contains the second datatable constaining
            //the Document Tracking notes data
            if (args.DataSetFromDataBase.Tables.Count < VendorInspVisitXmissionStructuresImageOrdinalPosition + 1)
            {
                ProjectCommon2.Helpers.ExceptionHelper.Throw(new ProjectCommon2.Exceptions.MissingDataTableException("VendorInspVisitXmissionStructuresImage not available for addition to VendorInspVisitXmissionStructuresResult entity."));
            }


            //create an object to hold the collection for addition to the entity 
            ProjectCommon2.Generics.EntityCollection< VendorInspVisitXmissionStructuresImageEntity> NewCollection;
            NewCollection = new ProjectCommon2.Generics.EntityCollection< VendorInspVisitXmissionStructuresImageEntity>(args.DataSetFromDataBase.Tables[1],args.UseCheckSum);


            //redeclare and cast the argument result colection
            ProjectCommon2.Generics.EntityCollection< VendorInspVisitXmissionStructuresResultEntity> ResultCollection;
            ResultCollection = (ProjectCommon2.Generics.EntityCollection< VendorInspVisitXmissionStructuresResultEntity>)args.CollectionToReturn;


            //loop through each entity in the resulting collection
            foreach (VendorInspVisitXmissionStructuresResultEntity ParentEntity in ResultCollection)
            {
                //create an entity that will be used to filter
                VendorInspVisitXmissionStructuresImageEntity filterEntity = new VendorInspVisitXmissionStructuresImageEntity();
                filterEntity.Vendor_InspVisit_XmissionStructures_ResultFk = ParentEntity.Vendor_InspVisit_XmissionStructures_ResultId;


                //create a filtered collection and add it to the entity
                ParentEntity.VendorInspVisitXmissionStructuresImageCollection = NewCollection.Filter(filterEntity, ProjectCommon2.Enumerations.FilterOperator.And);

            }

        }

        #endregion
		
        #region Start VendorInspVisitXmissionStructuresImage Transactions Event Handlers
		
		/// <summary>Handler for the <see cref="Classes.Factories.VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveCollectionBegin" />
		/// event. Responsible for notifying the VendorInspVisitXmissionStructuresResultFactory that originated the event if a transaction should
		/// be started.</summary>
		/// <param name="sender">The object that raised the event.</param>
        /// <param name="args">The object containing data about the event; must be of type 
		/// <see cref="Bases.BeforeSaveCollectionBeginEventArgs" />.</param>
		/// <remarks>
		/// <para>This method checks the <see cref="ProjectCommon2.Helpers.TransactionManager.IsInTransaction" />
		/// property of the static <c>TransactionManager</c> to see if a transaction has been already started, if not, it
		/// sets the <see cref="Bases.BeforeSaveCollectionBeginEventArgs.BeginTransaction" /> property of the event argument
		/// to <c>true</c>. This property is checked by the Factory upon resuming the Save operation to determine if it should
		/// start a new transaction.</para>
		/// </remarks>
        static void OnBeforeSaveVendorInspVisitXmissionStructuresImageCollectionBeginHandler(object sender, Classes.Bases.BeforeSaveCollectionBeginEventArgs args)
        {

            //if not already in a transaction then start one
            if (!ProjectCommon2.Helpers.TransactionManager.IsInTransaction)
            {
                args.BeginTransaction = true;
            }

        }

		/// <summary>Handler for the <see cref="Classes.Factories.VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveEntityBegin" />
		/// event. Responsible for notifying the VendorInspVisitXmissionStructuresResultFactory that originated the event if a transaction should
		/// be started.</summary>
		/// <param name="sender">The object that raised the event.</param>
        /// <param name="args">The object containing data about the event; must be of type 
		/// <see cref="Bases.BeforeSaveCollectionBeginEventArgs" />.</param>
		/// <remarks>
		/// <para>This method checks the <see cref="ProjectCommon2.Helpers.TransactionManager.IsInTransaction" />
		/// property of the static <c>TransactionManager</c> to see if a transaction has been already started, if not, it
		/// sets the <see cref="Bases.BeforeSaveCollectionBeginEventArgs.BeginTransaction" /> property of the event argument
		/// to <c>true</c>. This property is checked by the Factory upon resuming the Save operation to determine if it should
		/// start a new transaction.</para>
		/// </remarks>
        static void OnBeforeSaveVendorInspVisitXmissionStructuresImageEntityBeginHandler(object sender, Classes.Bases.BeforeSaveEntityBeginEventArgs args)
        {
            //if not already in a transaction then start one
            if (!ProjectCommon2.Helpers.TransactionManager.IsInTransaction)
            {
                args.BeginTransaction = true;
            }
        }

        #endregion

		#region Insert/Update/Delete VendorInspVisitXmissionStructuresImage Event Handlers
		
		/// <summary>Handler for the <see cref="VendorInspVisitXmissionStructuresResultFactory.OnBeforeEntityDelete" />
		/// event. Responsible for deleting all child VendorInspVisitXmissionStructuresImage Entities from the database before the 
		/// parent VendorInspVisitXmissionStructuresResultEntity is deleted.</summary>
		/// <param name="sender">The object that raised the event.</param>
        /// <param name="args">The object containing data about the event; must be of type 
		/// <see cref="Bases.BeforeEntityDeleteEventArgs" />.</param>
		/// <remarks>
		/// <para>Sets the deletion mark for every VendorInspVisitXmissionStructuresImageEntity in the child collection to
		/// <c>true</c> by calling the collection's 
		/// <see cref="ProjectCommon2.Generics.EntityCollection&lt;EntityType&gt;.SetAllIsMarkedToDelete(bool)" /> method.
		/// Then it deletes the collection by sending it to the VendorInspVisitXmissionStructuresImageFactory's
		/// <see cref="Factories.VendorInspVisitXmissionStructuresImageFactory.Save(ProjectCommon2.Generics.EntityCollection&lt;Classes.Entities.VendorInspVisitXmissionStructuresImageEntity&gt;, bool)" />
		/// method.</para>
		/// </remarks>
        static void OnBeforeVendorInspVisitXmissionStructuresImageEntityDeleteHandler(object sender, Classes.Bases.BeforeEntityDeleteEventArgs args)
        {

			//redeclare and cast the argument delete entity
            VendorInspVisitXmissionStructuresResultEntity DeleteEntity;
            DeleteEntity = (VendorInspVisitXmissionStructuresResultEntity)args.EntityToDelete;

			//make sure the child collection is actually used
			if (DeleteEntity.VendorInspVisitXmissionStructuresImageCollection == null)
				return;

            //mark all child entities of the parent entity to delete
            DeleteEntity.VendorInspVisitXmissionStructuresImageCollection.SetAllIsMarkedToDelete(true);

            //delete all the notes
            VendorInspVisitXmissionStructuresImageFactory.Save(DeleteEntity.VendorInspVisitXmissionStructuresImageCollection, args.UseCheckSum);

        }

        /// <summary>Handler for the <see cref="VendorInspVisitXmissionStructuresResultFactory.OnBeforeSaveEntityCommit" />
		/// event. Responsible for saving (which may mean inserting/updating/deleting) all child VendorInspVisitXmissionStructuresImage Entities
		/// to the database before the parent VendorInspVisitXmissionStructuresResultEntity is saved.</summary>
		/// <param name="sender">The object that raised the event.</param>
        /// <param name="args">The object containing data about the event; must be of type 
		/// <see cref="Bases.BeforeSaveEntityCommitEventArgs" />.</param>
		/// <remarks>
		/// <para>Saves every VendorInspVisitXmissionStructuresImageEntity in the child collection by sending it to the VendorInspVisitXmissionStructuresImageFactory's
		/// <see cref="Factories.VendorInspVisitXmissionStructuresImageFactory.Save(ProjectCommon2.Generics.EntityCollection&lt;Classes.Entities.VendorInspVisitXmissionStructuresImageEntity&gt;, bool)" />
		/// method.
		/// </para>
		/// </remarks>
        static void OnBeforeSaveVendorInspVisitXmissionStructuresImageEntityCommitHandler(object sender, Classes.Bases.BeforeSaveEntityCommitEventArgs args)
        {

			//redeclare and cast the argument save entity
            VendorInspVisitXmissionStructuresResultEntity EntityToSave;
            EntityToSave = (VendorInspVisitXmissionStructuresResultEntity)args.EntityToSave;

			//make sure the child collection is actually used
			if (EntityToSave.VendorInspVisitXmissionStructuresImageCollection == null)
				return;


            // loop through each entity in the VendorInspVisitXmissionStructuresImage collection
            foreach (VendorInspVisitXmissionStructuresImageEntity ChildEntity in EntityToSave.VendorInspVisitXmissionStructuresImageCollection)
            {
                //For Saftey set the FK for all the child entities
				ChildEntity.Vendor_InspVisit_XmissionStructures_ResultFk = EntityToSave.Vendor_InspVisit_XmissionStructures_ResultId;
				

            }

            // insert/update/delete all the child eities
            VendorInspVisitXmissionStructuresImageFactory.Save(EntityToSave.VendorInspVisitXmissionStructuresImageCollection, args.UseCheckSum);

        }

        #endregion
		
 		#endregion
		
	}
}